<template>
  <a-card :bordered="false">
    <a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
      <a-row :gutter="24">
        <a-col :span="6">
          <a-form-item label="时间" name="userId">
            <a-range-picker v-model:value="searchFormState.userId" show-time />
          </a-form-item>
        </a-col>
        <a-col :span="4">
          <a-form-item label="请假人" name="userId">
            <a-select v-model:value="searchFormState.userId" :options="optionsArr" placeholder="请假人">
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="4">
          <a-form-item label="原因" name="leaveType">
            <a-select v-model:value="searchFormState.leaveType" :options="leaveTypeList" placeholder="原因">
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="4">
          <a-form-item label="登记人" name="userId">
            <a-select v-model:value="searchFormState.userId" :options="optionsArr" placeholder="登记人">
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="4">
          <a-form-item label="査询时间" name="userId">
            <a-select v-model:value="searchFormState.userId" placeholder="査询时间">
              <a-select-option value="jack">开始时间</a-select-option>
              <a-select-option value="lucy">结束时间</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="4">
          <a-form-item label="状态査询" name="state">
            <a-select v-model:value="searchFormState.state" :options="stateList" placeholder="状态">
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="4">
          <a-button type="primary" @click="table.refresh(true)">查询</a-button>
          <a-button style="margin: 0 8px" @click="reset">重置</a-button>
        </a-col>
      </a-row>
    </a-form>
    <s-table ref="table" :columns="columns" :data="loadData" :alert="options.alert.show" bordered
      :row-key="(record) => record.id" :tool-config="toolConfig" :row-selection="options.rowSelection">
      <template #operator class="table-operator">
        <a-space>
          <a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('bizGoodsAdd')">
            <template #icon><plus-outlined /></template>
            新增
          </a-button>
          <xn-batch-delete v-if="hasPerm('bizGoodsBatchDelete')" :selectedRowKeys="selectedRowKeys"
            @batchDelete="deleteBatchBizGoods" />
        </a-space>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'categoryName'">
          {{ categoryIdOptions.filter(v => v.id == record.categoryId)[0].categoryName || '' }}
        </template>
        <template v-if="column.dataIndex === 'action'">
          <a-space>
            <a-popconfirm title="确定要销假吗？" @confirm="cancellationFakes(record)">
              <a-button type="link" danger size="small" v-if="hasPerm('bizGoodsDelete')">销假</a-button>
            </a-popconfirm>
            <a-popconfirm title="确定要作废吗？" @confirm="obsolete(record)">
              <a-button type="link" danger size="small" v-if="hasPerm('bizGoodsDelete')">作废</a-button>
            </a-popconfirm>
            <a @click="formRef.onOpen(record)" v-if="hasPerm('bizGoodsEdit')">编辑</a>
          </a-space>
        </template>
      </template>
    </s-table>
  </a-card>
  <Form ref="formRef" @successful="table.refresh(true)" />
</template>

<script setup name="goods">
import bizAttendanceApi from '@/api/biz/bizAttendanceApi'
import tool from '@/utils/tool'
import Form from './form.vue'
const leaveTypeList = [{
  label: '报停',
  value: 0
}, {
  label: '请假',
  value: 1
}, {
  label: '病假',
  value: 2
}, {
  label: '休息',
  value: 3
}, {
  label: '休假',
  value: 4
}, {
  label: '旷工',
  value: 5
}]

const stateList = [{
  label: '请假中',
  value: 0
}, {
  label: '已销假',
  value: 1
},
{
  label: '已作废',
  value: 2
},]
const rankedTypeList = [{
  label: '不动牌',
  value: 0
}, {
  label: '所在班次头牌',
  value: 1
}, {
  label: '所在班次尾牌',
  value: 2
}, {
  label: '全部头牌',
  value: 3
}, {
  label: '全部尾牌',
  value: 4
}, {
  label: '固定的位置',
  value: 5
}]
let searchFormState = reactive({
  leaveType: '',
  state: '',
})
let optionsArr = []
// const categoryIdOptions = tool.dictList('GOODSCATEGORY')
const categoryIdOptions = ref([])
const searchFormRef = ref()
const table = ref()
const formRef = ref()
const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
const columns = [
  {
    title: '状态',
    dataIndex: 'state'
  },
  {
    title: '工号',
    dataIndex: 'jobNo'
  },
  {
    title: '姓名',
    dataIndex: 'userName'
  },
  {
    title: '部门',
    dataIndex: 'sector'
  },
  {
    title: '开始时间',
    dataIndex: 'beganTime'
  },
  {
    title: '结束时间',
    dataIndex: 'endTime'
  },
  {
    title: '销假时间',
    dataIndex: 'eliminateTime'
  },
  {
    title: '实算天数',
    dataIndex: 'day'
  },
  {
    title: '原因',
    dataIndex: 'leaveType'
  },
  {
    title: '牌位情况',
    dataIndex: 'rankedType'
  },
  {
    title: '登记人',
    dataIndex: 'description'
  },
  {
    title: '登记时间',
    dataIndex: 'description'
  },
]
// 操作栏通过权限判断是否显示
if (hasPerm(['bizGoodsEdit', 'bizGoodsDelete'])) {
  columns.push({
    title: '操作',
    dataIndex: 'action',
    align: 'center',
    width: '150px'
  })
}
const selectedRowKeys = ref([])
// 列表选择配置
const options = {
  // columns数字类型字段加入 needTotal: true 可以勾选自动算账
  alert: {
    show: true,
    clear: () => {
      selectedRowKeys.value = ref([])
    }
  },
  rowSelection: {
    onChange: (selectedRowKey, selectedRows) => {
      selectedRowKeys.value = selectedRowKey
    }
  }
}
const loadData = (parameter) => {
  const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
  return bizAttendanceApi.bizAttendancePage(Object.assign(parameter, searchFormParam)).then((data) => {
    return data
  })
}
const cancellationFakes = (item)=>{
  bizAttendanceApi.bizAttendanceCancellationFakes({id:item.id}).then(res=>{
    console.log(res)
    loadData()
  })
}
const obsolete = (item)=>{
  bizAttendanceApi.bizAttendanceObsolete({id:item.id}).then(res=>{
    console.log(res)
    loadData()
  })
}
// 重置
const reset = () => {
  searchFormRef.value.resetFields()
  table.value.refresh(true)
}
// 删除
const deleteBizGoods = (record) => {
  let params = [
    {
      id: record.id
    }
  ]
  bizAttendanceApi.bizAttendanceDelete(params).then(() => {
    table.value.refresh(true)
  })
}
// 批量删除
const deleteBatchBizGoods = (params) => {
  bizAttendanceApi.bizAttendanceDelete(params).then(() => {
    table.value.clearRefreshSelected()
  })
}
</script>